package com.novel.frame.lxf;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class CodeMaker{
       
	 //生成随机类
    Random random = new Random();
     
    //验证码字体
    private Font[] codeFont = {
        new Font("Times New Roman", Font.PLAIN,30), 
        new Font("Times New Roman", Font.PLAIN, 30),
        new Font("Times New Roman", Font.PLAIN,30),
        new Font("Times New Roman", Font.PLAIN, 30)
    };
    
    private static String[] codes = new String[62];
    static {
    	int index = 0;
    	for (int i = 0; i <= 9; i++,index++) {
    		codes[index] = i+"";
		}
    	//大写字母
    	for (int i = 65; i <= 90; i++,index++) {
    		codes[index] = (char)i+"";
		}
    	//小写字母
    	for (int i = 97; i <= 122; i++,index++) {
    		codes[index] = (char)i+"";
		}
    }
    
    //验证码数字颜色
    private Color[] color = {
       getRandColor(0, 255), getRandColor(0, 255), getRandColor(0, 255), getRandColor(0, 255)
    };
 
    String codeNumbers = "";
    private String validCode;//保存验证码
    public String getValidCode() {
		return validCode;
	}
	public void setValidCode(String validCode) {
		this.validCode = validCode;
	}

	int width = 100, height = 30;
 
    public void createValidCode(String savePath) throws IOException {
 
        // 创建一个图像,验证码显示的图片大小
        BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
 
        // 获取图形上下文
        Graphics g = image.getGraphics();
         
        // 设置背景
        g.setColor(getRandColor(200,250));
        g.fillRect(0, 0, width, height);
 
        for (int i = 0; i < 4; i++) {
            drawCode(g, i);
        }
        //添加干扰线
        drawNoise(g, 12);
 
        // 绘制边框
        //g.setColor(Color.gray);
        //g.drawRect(0, 0, width - 1, height - 1);
        validCode = codeNumbers;//保存验证码
        codeNumbers = "";
        // 利用ImageIO类的write方法对图像进行编码
        OutputStream output = new FileOutputStream(savePath);
        ImageIO.write(image, "PNG", output);
    }
 
    // 绘制验证码
    public void drawCode(Graphics graphics, int i) {
        //String number = String.valueOf(random.nextInt(10));
    	String number = codes[random.nextInt(codes.length)];
        graphics.setFont(codeFont[i]);
        graphics.setColor(color[i]);
        //绘制验证码到图片X、Y
        graphics.drawString(number, 13 + i * 20,25);
 
        codeNumbers += number;
    }
 
    // 绘制干扰线
    public void drawNoise(Graphics graphics, int lineNumber) {
        graphics.setColor(getRandColor(160,200));
        for (int i = 0; i < lineNumber; i++) {
            int pointX1 = 1 + (int)(Math.random() * width);
            int pointY1 = 1 + (int)(Math.random() * height);
            int pointX2 = 1 + (int)(Math.random() * width);
            int pointY2 = 1 + (int)(Math.random() * height);
            graphics.drawLine(pointX1, pointY1, pointX2, pointY2);
        }
    }
    
    public Color getRandColor(int fc,int bc) {//给定范围获得随机颜色
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
        return new Color(r,g,b);
    }
    
    public static void main(String[] args) {
//    	for (int i = 0; i < CodeMaker.codes.length; i++) {
//			System.out.println(CodeMaker.codes[i]);
//		}
    	CodeMaker cm = new CodeMaker();
    	File VirificodePicture=new File("D:\\工作空间\\VirificodePicture");
    	if (!VirificodePicture.exists()) {
    		VirificodePicture.mkdir();
		}
    	try {
    		cm.width = 120;
    		cm.height = 40;
    		String path = "D:\\VirificodePicture\\验证码图片.png";
			cm.createValidCode(path);
			System.out.println("验证码生成成功！");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
} 